package vistas;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.nio.charset.CodingErrorAction;
import java.sql.ResultSet;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.table.DefaultTableModel;
import javax.swing.ImageIcon;

import clases.CV_imprimir;
import clases.Ficha_class;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Cursor;
import java.awt.Toolkit;
import javax.swing.JScrollPane;

public class Buscafichaform extends JInternalFrame {

	private final JPanel contentPanel = new JPanel();
	private JTextField tbnombres;
	JFormattedTextField tbcodigo;
	private JTable table;
	private int filas;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Buscafichaform dialog = new Buscafichaform(0);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 * @throws ParseException 
	 */
	public Buscafichaform(int tipo) throws ParseException {
		setFrameIcon(new ImageIcon(Buscafichaform.class.getResource("/icons/ficha.png")));
		setClosable(true);
		setIconifiable(true);
		setTitle("Buscar Ficha");
		filas=0;
		setBounds(100, 100, 715, 433);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel label = new JLabel("Codigo");
			label.setFont(new Font("Tahoma", Font.PLAIN, 13));
			label.setBounds(10, 13, 39, 16);
			contentPanel.add(label);
		}
		{
			tbcodigo = new JFormattedTextField();
			tbcodigo.setFont(new Font("Tahoma", Font.PLAIN, 13));
			tbcodigo.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-######-###-#")));
			tbcodigo.setBounds(59, 11, 125, 20);
			contentPanel.add(tbcodigo);
		}
		{
			JLabel label = new JLabel("Nombres");
			label.setFont(new Font("Tahoma", Font.PLAIN, 13));
			label.setBounds(194, 13, 59, 16);
			contentPanel.add(label);
		}
		{
			tbnombres = new JTextField();
			tbnombres.setFont(new Font("Tahoma", Font.PLAIN, 13));
			tbnombres.setColumns(10);
			tbnombres.setBounds(263, 11, 308, 20);
			contentPanel.add(tbnombres);
		}
		
		JButton btnBuscar = new JButton("Buscar");
		btnBuscar.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		btnBuscar.setContentAreaFilled(false);
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				metodoBuscar();
			}
		});
		btnBuscar.setIcon(new ImageIcon(Buscafichaform.class.getResource("/icons/buscar.png")));
		btnBuscar.setBounds(581, 11, 108, 23);
		contentPanel.add(btnBuscar);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 40, 679, 344);
		contentPanel.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Codigo", "Nombre"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		JMenuItem mntmVer = new JMenuItem("Ver");
		mntmVer.setIcon(new ImageIcon(Buscafichaform.class.getResource("/icons/modificar_pq.png")));
		mntmVer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(table.getSelectedRow()>-1)
				{
					try
					{
						Ingresarfichaform ficha = new Ingresarfichaform(table.getValueAt(table.getSelectedRow(), 0).toString());
						Principal.contenedor.add(ficha);
						ficha.setVisible(true);
					}catch(Exception ex)
					{
						JOptionPane.showMessageDialog(null, ex.toString());
					}
				}
			}
		});
		popupMenu.add(mntmVer);
		
		JMenuItem mntmImprimir = new JMenuItem("Imprimir");
		mntmImprimir.setIcon(new ImageIcon(Buscafichaform.class.getResource("/icons/imprimir_pq.png")));
		mntmImprimir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(table.getSelectedRow()>-1)
				{
					CV_imprimir imprimir = new CV_imprimir();
					imprimir.crearDocumento(String.valueOf(table.getValueAt(table.getSelectedRow(), 0)));
				}
			}
		});
		popupMenu.add(mntmImprimir);
		
		if(tipo==1)
		{
			JMenuItem mntmSeleccionar_1 = new JMenuItem("Seleccionar");
			mntmSeleccionar_1.setIcon(new ImageIcon(Buscafichaform.class.getResource("/icons/seleccionar_pq.png")));
			mntmSeleccionar_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Asignarhorarios_ingresarform.templeado.setText(table.getValueAt(table.getSelectedRow(), 1).toString());
					Asignarhorarios_ingresarform.codigoempleado=(table.getValueAt(table.getSelectedRow(), 0).toString());
					dispose();
				}
			});
			popupMenu.add(mntmSeleccionar_1);
		}
		
		metodoBuscar();		
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	private void metodoBuscar()
	{

		try
		{
			DefaultTableModel mficha = (DefaultTableModel) table.getModel();
			while(mficha.getRowCount()>0) mficha.removeRow(0);
			filas=0;
			ResultSet resultado = new Ficha_class(tbcodigo.getText(),tbnombres.getText()).buscar();
			while(resultado.next())
			{
				mficha.addRow(new Object[filas]);
				mficha.setValueAt(resultado.getString("codigonit"), filas, 0);
				mficha.setValueAt(resultado.getString("Nombres")+ " "+resultado.getString("primerapellido")+" "+resultado.getString("segundoapellido"), filas, 1);
				filas++;
			}
			resultado.close();
		}catch(Exception ex){JOptionPane.showConfirmDialog(null, ex.toString());}
	
	}
	
}
